package com.gisocn.onemap.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.core.app.ActivityCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.gisocn.onemap.base.BaseFragment;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.base.MyApplication;
import com.gisocn.onemap.event.MessageEvent;
import com.gisocn.onemap.greendao.OfflineDownLoadFileInfoDao;
import com.gisocn.onemap.interfaces.DownLoadListener;
import com.gisocn.onemap.interfaces.OnItemClickListener;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.Download;
import com.gisocn.onemap.model.LoadResponseHead;
import com.gisocn.onemap.model.OfflineDownLoadFileInfo;
import com.gisocn.onemap.presenter.OfflineDownloadPresenter;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.ui.activity.MainActivity;
import com.gisocn.onemap.ui.adapter.OfflineDownloadAdapter;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.view.OfflineDownloadView;
import com.google.gson.Gson;
import com.sangfor.ssl.common.Foreground;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.greenrobot.eventbus.EventBus;

/* loaded from: classes2.dex */
public class OfflineDownloadFragment extends BaseFragment<OfflineDownloadPresenter> implements OfflineDownloadView {
    public static final int EXTERNAL_STORAGE_REQ_CODE = 10;
    private static Gson gson = new Gson();
    private OfflineDownloadAdapter adapter;
    private AppConfig appConfig;
    OfflineDownloadPresenter presenter;
    RecyclerView recycleOfflineDownload;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    Unbinder unbinder;
    private ArrayList<Download.NewItem> xmlList = new ArrayList<>();
    private ArrayList<Download.NewItem> localList = new ArrayList<>();
    private ArrayList<Download.NewItem> showList = new ArrayList<>();
    private ArrayList<Download.NewItem> netList = new ArrayList<>();
    ArrayList<Download.NewItem> downloadInfoList = new ArrayList<>();
    private OfflineDownLoadFileInfoDao fileInfoDao = MyApplication.getInstances().getDaoSession().getOfflineDownLoadFileInfoDao();
    Handler handler = new Handler() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadFragment.3
        @Override // android.os.Handler
        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            int i2 = 0;
            if (i == 1) {
                OfflineDownloadFragment.this.dismissProgressDialog();
                Iterator it = OfflineDownloadFragment.this.showList.iterator();
                while (it.hasNext()) {
                    Download.NewItem newItem = (Download.NewItem) it.next();
                    String str = newItem.getcData().trim().split("/")[newItem.getcData().trim().split("/").length - 1];
                    L.e("fileName" + str);
                    if (!newItem.getName().contains("矢量数据")) {
                        if (!new File(Constant.SAVE_PATH_TPK, str).exists()) {
                            newItem.setVersionDesc("未下载");
                        } else {
                            if (!newItem.getVersionDesc().equals("可更新")) {
                                newItem.setVersionDesc("已是最新版本");
                                L.e("");
                            }
                            L.e(str + "文件存在:" + newItem.getVersion() + "" + newItem.getcData() + "|" + newItem.getVersionDesc());
                        }
                    } else {
                        File[] listFiles = new File(Constant.SAVE_PATH_SHP).listFiles();
                        if (listFiles == null || listFiles.length == 0) {
                            newItem.setVersionDesc("未下载");
                        } else if (listFiles.length == 1) {
                            if ((listFiles.length == 1 && listFiles[0].getName().contains(".zip")) || listFiles[0].getName().contains(".temp")) {
                                newItem.setVersionDesc("未下载");
                            }
                        } else if (!newItem.getVersionDesc().equals("可更新")) {
                            newItem.setVersionDesc("已是最新版本");
                        }
                    }
                    L.e("item.getName()" + newItem.getName());
                    L.e("item.getcData()" + newItem.getcData());
                }
                while (i2 < OfflineDownloadFragment.this.showList.size()) {
                    L.e(((Download.NewItem) OfflineDownloadFragment.this.showList.get(i2)).getName() + ".getSize()" + ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i2)).getSize());
                    if (((Download.NewItem) OfflineDownloadFragment.this.showList.get(i2)).getSize() != null && Long.parseLong(((Download.NewItem) OfflineDownloadFragment.this.showList.get(i2)).getSize()) < Foreground.CHECK_DELAY) {
                        OfflineDownloadFragment.this.showList.remove(i2);
                        i2--;
                    }
                    i2++;
                }
                OfflineDownloadFragment.this.initView();
                Log.e("showList.size()", OfflineDownloadFragment.this.showList.size() + "");
            } else if (i == 2) {
                OfflineDownloadFragment.this.adapter.notifyItemChanged(message.arg2, 0);
                if (message.arg1 == 100) {
                    OfflineDownloadFragment.this.adapter.notifyDataSetChanged();
                    EventBus.getDefault().post(new MessageEvent(Constant.MESSAGE_EVENT_UPDATA_LAYER_DATA));
                }
            } else if (i != 3) {
                if (i != 4) {
                    return;
                }
                OfflineDownloadFragment.this.dismissProgressDialog();
                OfflineDownloadFragment.this.showToast("下载失败");
            } else {
                L.e("showList.size():" + OfflineDownloadFragment.this.showList.size() + "netList.size():" + OfflineDownloadFragment.this.netList.size());
                if (OfflineDownloadFragment.this.localList.size() > 0) {
                    OfflineDownloadFragment.this.downloadInfoList.addAll(OfflineDownloadFragment.this.localList);
                    L.e("localList" + OfflineDownloadFragment.this.localList.size());
                    while (i2 < OfflineDownloadFragment.this.localList.size()) {
                        if (OfflineDownloadFragment.this.downloadInfoList.get(i2).getName().equals(((Download.NewItem) OfflineDownloadFragment.this.showList.get(message.arg1)).getName())) {
                            OfflineDownloadFragment.this.downloadInfoList.get(i2).setVersionDesc("已是最新版本");
                            OfflineDownloadFragment.this.downloadInfoList.get(i2).setCurrentLength("100");
                            Iterator it2 = OfflineDownloadFragment.this.netList.iterator();
                            while (true) {
                                if (it2.hasNext()) {
                                    Download.NewItem newItem2 = (Download.NewItem) it2.next();
                                    if (((Download.NewItem) OfflineDownloadFragment.this.showList.get(message.arg1)).getName().equals(newItem2.getName())) {
                                        OfflineDownloadFragment.this.downloadInfoList.get(i2).setVersion(newItem2.getVersion());
                                        break;
                                    }
                                }
                            }
                        } else {
                            Download.NewItem newItem3 = new Download.NewItem();
                            newItem3.setName(((Download.NewItem) OfflineDownloadFragment.this.showList.get(message.arg1)).getName());
                            newItem3.setVersionDesc("已是最新版本");
                            newItem3.setVersion(((Download.NewItem) OfflineDownloadFragment.this.netList.get(message.arg1)).getVersion());
                            newItem3.setCurrentLength("100");
                            Iterator it3 = OfflineDownloadFragment.this.netList.iterator();
                            while (true) {
                                if (!it3.hasNext()) {
                                    break;
                                }
                                Download.NewItem newItem4 = (Download.NewItem) it3.next();
                                if (((Download.NewItem) OfflineDownloadFragment.this.showList.get(message.arg1)).getName().equals(newItem4.getName())) {
                                    newItem3.setVersion(newItem4.getVersion());
                                    break;
                                }
                            }
                            OfflineDownloadFragment.this.downloadInfoList.add(newItem3);
                        }
                        i2++;
                    }
                } else {
                    Download.NewItem newItem5 = new Download.NewItem();
                    newItem5.setName(((Download.NewItem) OfflineDownloadFragment.this.showList.get(message.arg1)).getName());
                    newItem5.setVersionDesc("已是最新版本");
                    Iterator it4 = OfflineDownloadFragment.this.netList.iterator();
                    while (true) {
                        if (!it4.hasNext()) {
                            break;
                        }
                        Download.NewItem newItem6 = (Download.NewItem) it4.next();
                        if (((Download.NewItem) OfflineDownloadFragment.this.showList.get(message.arg1)).getName().equals(newItem6.getName())) {
                            newItem5.setVersion(newItem6.getVersion());
                            break;
                        }
                    }
                    newItem5.setVersion(((Download.NewItem) OfflineDownloadFragment.this.netList.get(message.arg1)).getVersion());
                    newItem5.setCurrentLength("100");
                    OfflineDownloadFragment.this.downloadInfoList.add(newItem5);
                }
                if (OfflineDownloadFragment.this.downloadInfoList.size() > 0) {
                    Iterator<Download.NewItem> it5 = OfflineDownloadFragment.this.downloadInfoList.iterator();
                    while (it5.hasNext()) {
                        Download.NewItem next = it5.next();
                        OfflineDownLoadFileInfo offlineDownLoadFileInfo = new OfflineDownLoadFileInfo();
                        offlineDownLoadFileInfo.setCData(next.getcData());
                        offlineDownLoadFileInfo.setCurrentLength(next.getCurrentLength());
                        offlineDownLoadFileInfo.setLabel(next.getLabel());
                        offlineDownLoadFileInfo.setLastModifiedTime(next.getLastModifiedTime());
                        offlineDownLoadFileInfo.setName(next.getName());
                        offlineDownLoadFileInfo.setType(next.getType());
                        offlineDownLoadFileInfo.setSize(next.getSize());
                        offlineDownLoadFileInfo.setVersion(next.getVersion());
                        offlineDownLoadFileInfo.setVersionDesc(next.getVersionDesc());
                        OfflineDownloadFragment.this.fileInfoDao.insertOrReplace(offlineDownLoadFileInfo);
                    }
                }
            }
        }
    };

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDownLoadSuccess(String str, int i) {
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void onInvisible() {
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_offline_download, viewGroup, false);
        this.unbinder = ButterKnife.bind(this, inflate);
        this.tvTitle.setText(R.string.offline_download);
        return inflate;
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void initData() {
        this.appConfig = ((MainActivity) getActivity()).getAppConfig();
        this.presenter = createPresenter();
        if (this.appConfig.getDownload().getItems() == null || this.appConfig.getDownload().getItems().size() <= 0) {
            return;
        }
        this.xmlList.clear();
        this.netList.clear();
        this.showList.clear();
        Iterator<Download.NewItem> it = this.appConfig.getDownload().getItems().iterator();
        while (it.hasNext()) {
            Download.NewItem next = it.next();
            this.xmlList.add(next);
            this.netList.add(next);
            next.setVersionDesc("未下载");
            this.showList.add(next);
        }
        List<OfflineDownLoadFileInfo> loadAll = this.fileInfoDao.loadAll();
        if (loadAll.size() > 0) {
            for (OfflineDownLoadFileInfo offlineDownLoadFileInfo : loadAll) {
                Download.NewItem newItem = new Download.NewItem();
                newItem.setcData(offlineDownLoadFileInfo.getCData());
                newItem.setCurrentLength(offlineDownLoadFileInfo.getCurrentLength());
                newItem.setLabel(offlineDownLoadFileInfo.getLabel());
                newItem.setLastModifiedTime(offlineDownLoadFileInfo.getLastModifiedTime());
                newItem.setName(offlineDownLoadFileInfo.getName());
                newItem.setType(offlineDownLoadFileInfo.getType());
                newItem.setSize(offlineDownLoadFileInfo.getSize());
                newItem.setVersion(offlineDownLoadFileInfo.getVersion());
                newItem.setVersionDesc(offlineDownLoadFileInfo.getVersionDesc());
                this.localList.add(newItem);
            }
        }
        showProgressDialog();
        if (ActivityCompat.checkSelfPermission(getActivity(), "android.permission.WRITE_EXTERNAL_STORAGE") == 0) {
            new Thread(new Runnable() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadFragment.1
                @Override // java.lang.Runnable
                public void run() {
                    OfflineDownloadFragment.this.presenter.getDownLoadInfo(((Download.NewItem) OfflineDownloadFragment.this.xmlList.get(0)).getcData().trim(), 0);
                }
            }).start();
        } else {
            showToast("请设置文件读写权限");
        }
    }

    void initView() {
        dismissProgressDialog();
        Log.e("init", "init");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(1);
        this.recycleOfflineDownload.setLayoutManager(linearLayoutManager);
        OfflineDownloadAdapter offlineDownloadAdapter = new OfflineDownloadAdapter(this.showList, getActivity());
        this.adapter = offlineDownloadAdapter;
        this.recycleOfflineDownload.setAdapter(offlineDownloadAdapter);
        final ArrayList arrayList = new ArrayList();
        this.adapter.setOnItemClickListener(new OnItemClickListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadFragment.2
            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, int i, int i2) {
            }

            @Override // com.gisocn.onemap.interfaces.OnItemClickListener
            public void onItemClick(View view, final int i) {
                String str;
                Log.e("onItemClick", "onItemClick" + ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getcData());
                if (arrayList.contains(Integer.valueOf(i))) {
                    Message message = new Message();
                    message.what = 2;
                    OfflineDownloadFragment offlineDownloadFragment = OfflineDownloadFragment.this;
                    message.arg1 = offlineDownloadFragment.isNull(((Download.NewItem) offlineDownloadFragment.showList.get(i)).getCurrentLength()) ? 0 : Integer.parseInt(((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getCurrentLength());
                    message.arg2 = i;
                    OfflineDownloadFragment.this.handler.sendMessage(message);
                    return;
                }
                arrayList.add(Integer.valueOf(i));
                Message message2 = new Message();
                String trim = ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getcData().trim();
                L.e("showList.get(position).getCurrentLength" + ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getCurrentLength());
                L.e("showList.get(position).getCurrentLength" + ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getSize());
                String str2 = trim.split("/")[trim.split("/").length + (-1)] + ".temp";
                L.e("showList.get(position).getCurrentLength" + str2);
                if (str2.contains(".tpk")) {
                    str = Constant.SAVE_PATH_TPK + str2;
                } else {
                    str = Constant.SAVE_PATH_SHP + str2;
                }
                File file = new File(str);
                if (file.exists()) {
                    L.e("存在临时文件大小" + file.length() + "---" + Float.parseFloat(((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getSize()));
                    r0 = (int) ((((float) file.length()) / Float.parseFloat(((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getSize())) * 100.0f);
                    StringBuilder sb = new StringBuilder();
                    sb.append("currentLength===========");
                    sb.append(((float) file.length()) / Float.parseFloat(((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getSize()));
                    L.e(sb.toString());
                }
                ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).setCurrentLength("" + r0);
                L.e("开始进度currentLength" + r0);
                message2.what = 2;
                message2.arg2 = i;
                message2.arg1 = r0;
                OfflineDownloadFragment.this.handler.sendMessage(message2);
                OfflineDownloadFragment.this.presenter.getDownLoading(((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).getcData().trim(), new DownLoadListener() { // from class: com.gisocn.onemap.ui.fragment.OfflineDownloadFragment.2.1
                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onStart() {
                        L.e("onStart____________________");
                    }

                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onProgress(int i2) {
                        OfflineDownloadFragment.this.dismissProgressDialog();
                        ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).setCurrentLength("" + i2);
                        if (i2 == 100) {
                            ((Download.NewItem) OfflineDownloadFragment.this.showList.get(i)).setVersionDesc("已是最新版本");
                        }
                        Message message3 = new Message();
                        message3.what = 2;
                        message3.arg1 = i2;
                        message3.arg2 = i;
                        OfflineDownloadFragment.this.handler.sendMessage(message3);
                    }

                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onFinish(String str3) {
                        Message message3 = new Message();
                        message3.what = 3;
                        message3.arg1 = i;
                        OfflineDownloadFragment.this.handler.sendMessage(message3);
                    }

                    @Override // com.gisocn.onemap.interfaces.DownLoadListener
                    public void onFailure() {
                        OfflineDownloadFragment.this.handler.sendEmptyMessage(4);
                    }
                });
            }
        });
    }

    @Override // androidx.fragment.app.Fragment
    public void onStop() {
        super.onStop();
        dismissProgressDialog();
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.gisocn.onemap.base.BaseFragment
    public OfflineDownloadPresenter createPresenter() {
        return new OfflineDownloadPresenter(this, getActivity());
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public void onDestroyView() {
        super.onDestroyView();
        this.unbinder.unbind();
        OfflineDownloadPresenter offlineDownloadPresenter = this.presenter;
        if (offlineDownloadPresenter != null) {
            offlineDownloadPresenter.closeDownloading();
        }
        this.handler.removeCallbacksAndMessages(null);
    }

    @OnClick({R.id.tv_close, R.id.tv_right})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.tv_close) {
            return;
        }
        getActivity().findViewById(R.id.viewPager).setVisibility(8);
    }

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDownLoadInfoSuccess(LoadResponseHead loadResponseHead, int i) {
        if (!isNull(loadResponseHead.getLast_modified())) {
            String last_modified = loadResponseHead.getLast_modified();
            this.netList.get(i).setVersion(String.valueOf(new Date(last_modified).getTime() / 1000));
            L.e(this.netList.get(i).getName() + "dateStr" + String.valueOf(new Date(last_modified).getTime() / 1000));
        }
        if (!isNull(loadResponseHead.getContent_length())) {
            this.showList.get(i).setSize(loadResponseHead.getContent_length());
        }
        int i2 = i + 1;
        if (i2 <= this.xmlList.size() - 2) {
            if (this.xmlList.get(i2).getcData() != null) {
                this.presenter.getDownLoadInfo(this.xmlList.get(i2).getcData().trim(), i2);
                return;
            }
            int i3 = i2 + 1;
            this.presenter.getDownLoadInfo(this.xmlList.get(i3).getcData().trim(), i3);
            return;
        }
        if (this.localList.size() > 0) {
            for (int i4 = 0; i4 < this.netList.size(); i4++) {
                if (isNull(this.netList.get(i4).getVersion())) {
                    this.showList.get(i4).setVersionDesc("已是最新版本");
                } else {
                    Iterator<Download.NewItem> it = this.localList.iterator();
                    while (it.hasNext()) {
                        Download.NewItem next = it.next();
                        L.e("本地文件" + next.getName());
                        if (next.getName().equals(this.showList.get(i4).getName())) {
                            if (this.netList.get(i4).getVersion() != null) {
                                L.e(this.netList.get(i4).getName() + "getVersion" + this.netList.get(i4).getVersion());
                                if (isNull(next.getVersion()) || next.getVersion().equals("未下载")) {
                                    this.showList.get(i4).setVersionDesc("未下载");
                                } else if (Long.parseLong(next.getVersion()) == Long.parseLong(this.netList.get(i4).getVersion())) {
                                    this.showList.get(i4).setVersionDesc("已是最新版本");
                                } else if (Long.parseLong(next.getVersion()) < Long.parseLong(this.netList.get(i4).getVersion())) {
                                    this.showList.get(i4).setVersionDesc("可更新");
                                }
                            } else {
                                this.showList.get(i4).setVersionDesc("已是最新版本");
                            }
                        }
                    }
                }
            }
        }
        this.handler.sendEmptyMessage(1);
    }

    @Override // com.gisocn.onemap.view.OfflineDownloadView
    public void getDataFail(String str) {
        hideLoading();
        showToast(str);
    }

    @Override // com.gisocn.onemap.base.BaseFragment, com.gisocn.onemap.base.BaseView
    public void showLoading() {
        showProgressDialog();
        Log.e("showLoading", "showLoading");
    }

    @Override // com.gisocn.onemap.base.BaseFragment, com.gisocn.onemap.base.BaseView
    public void hideLoading() {
        dismissProgressDialog();
        Log.e("hideLoading", "hideLoading");
    }
}
